# -*- coding: utf-8 -*-

import cgi
import sys, os
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import facebook
from facebook import FacebookError

FACEBOOK_API_KEY = ''
FACEBOOK_SECRET_KEY = ''
_DEBUG = True

class MainPage(webapp.RequestHandler):
    #def get(self):
    def post(self):
        self.response.out.write('hello post<fb:name uid="loggedinuser"/>')
        
        fb = facebook.Facebook(FACEBOOK_API_KEY, FACEBOOK_SECRET_KEY)
        if fb.check_session(self.request) and fb.added:
	        pass
    	else:
            url = fb.get_add_url()
            self.response.out.write('<script language="javascript">top.location.href="' + url + '"</script>')
            return

    #get friend list begin
        friend_ids = fb.friends.get()
        friend_ids.append(fb.uid)
        
        info = fb.users.getInfo(friend_ids, ['name', 'pic_square', 'uid'])
        template_values = {}
        template_values = {'info': info}
    #get friend list end
        for i in info:
            self.response.out.write('<fb:name uid=%s>' % (i['uid']))

def main():
    application = webapp.WSGIApplication([('/', MainPage)], debug=_DEBUG)
    wsgiref.handlers.CGIHandler().run(application)
    
if __name__ == "__main__":
    main()
 

